import org.apache.commons.lang3.StringUtils;

public class Db33 {

  // 打印sql
  public static void main(String[] args) {
    Db33 db2 = new Db33();
    String sql =
        db2.getSql(null, null, null, null, null, null, null, "440111103218006,440111103218009");
    System.out.println(sql);
  }

  /**
   * 获得要执行的SQL脚本
   *
   * @param
   * @return 需要执行的SQL脚本
   */
  //    @Override
  //    public String getSql(DbNode node, Map<String, NodeParam> binds){
  //        JSONObject in = getIn(binds);
  //        String xzqmc = in.getString("xzqmc");
  //        String jzmc = in.getString("jzmc");
  //        String czcmc = in.getString("czcmc");
  //        String wgmc = in.getString("wgmc");
  //        String xzqdm = in.getString("xzqdm");
  //        String jzdm = in.getString("jzdm");
  //        String czcdm = in.getString("czcdm");
  //        String wgdm = in.getString("wgdm");
  //        return getSql(xzqmc,jzmc,czcmc,wgmc,xzqdm,jzdm,czcdm,wgdm);
  //    }

  public String getSql(
      String xzqmc,
      String jzmc,
      String czcmc,
      String wgmc,
      String xzqdm,
      String jzdm,
      String czcdm,
      String wgdm) {
    final String unionAll = " union all ";
    String sql1 =
        " select count(DISTINCT(ZJHM)) as zs,'工地从业人员' as name,'gdcyry' as code from ads_czc_dw_cyryxx\n"
            + "where czcdm is not null "
            + getSearchSql("xzqmc", xzqmc)
            + getSearchSql("jzmc", jzmc)
            + getSearchSql("czcmc", czcmc)
            + getSearchSql("wgmc", wgmc)
            + getSearchSql("xzqdm", xzqdm)
            + getSearchSql("jzdm", jzdm)
            + getSearchSql("czcdm", czcdm)
            + getInSql(wgdm);

    return sql1;
  }

  // 获取前端传过来的入参
  //    public static JSONObject getIn(Map<String, NodeParam> params){
  //        NodeParam input = params.get("input");
  //        JSONObject json = (JSONObject) JSON.toJSON(input.getIn());
  //        return json;
  //    }
  public String getSearchSql(String column, String value) {
    String sql = " and 1=1 ";
    if (StringUtils.isNotBlank(value)) {
      if ("其他".equals(value)) {
        sql = " and " + column + " is null ";
      } else {
        sql = " and " + column + "  =  " + "'" + value + "'";
      }
    }
    return sql;
  }

  public String getInSql(String wgdm) {
    String and = " and 1=1 ";
    if (StringUtils.isNotEmpty(wgdm)) {
      StringBuilder sb = new StringBuilder();
      String wgdmStr = " and wgdm in ";
      String[] strs = wgdm.split(",");
      sb.append("(");
      for (int i = 0; i < strs.length; i++) {
        sb.append("'").append(strs[i]).append("'"); // 拼接单引号,到数据库后台用in查询.
        if (i != strs.length - 1) { // 前面的元素后面全拼上",",最后一个元素后不拼
          sb.append(",");
        }
      }
      sb.append(")");
      return and + wgdmStr + sb;
    }
    return and;
  }
}
